﻿@model  DHQGHN.DAL.Common.DataPage<DHQGHN.PM0.Models.Admin.LogModel>
@{
    var pagNar = ViewBag.Pagination as DHQGHN.DAL.Common.Pagination;
    var lstItem = Model.Data;
}
<table class="tbl">
    <thead>
        <tr>
            <th style="width:4%">STT</th>
            <th style="width:4%">Chọn</th>
            <th style="width:10%">Tài khoản</th>
            <th style="width:12%">Phần mềm</th>
            <th style="width:13%">Chức năng</th>
            <th style="width:20%">Dữ liệu</th>
            <th style="width:15%">Action</th>
            <th style="width:10%">Action time</th>
            <th style="width:5%">Chi tiết</th>
        </tr>
    </thead>
    <tbody id="ctg_content_body">
        @if (Model.Data.Any())
        {
            for (int i = 0; i < lstItem.Count; i++)
            {
                <tr>
                    <td class="align-center">@(i + 1 + pagNar.CurrentPage * pagNar.PageSize)</td>
                    <td style="text-align:center">
                        <div id="checkbox"><input type="checkbox" name="@lstItem[i].ID" /></div>
                    </td>
                    <td style="text-align: left;">@lstItem[i].Account</td>
                    <td style="text-align: left;">@lstItem[i].AppName</td>
                    <td style="text-align: left;">@lstItem[i].Function</td>
                    <td style="text-align: left;">@lstItem[i].Data</td>
                    <td style="text-align: left;">@lstItem[i].Action</td>
                    <td style="text-align: left;">@lstItem[i].ActionTime</td>
                    <td style="text-align: center;">
                        <a title='Xem chi tiết' href='javascript:void(0)' class='view-detail' data-id='@lstItem[i].ID'>
                            <div class='glyphicon glyphicon-screenshot'></div>
                        </a>
                    </td>
                </tr>
            }
        }
        else
        {
            <tr>
                <td colspan="9"><p>Không tìm thấy thông tin ghi log nào.</p></td>
            </tr>
        }
    </tbody>
    <tfoot>
        <tr>
            <td colspan="9" style="text-align:left;height:25px">
                <button type="button" class="btn btn-danger" title="Xóa log" id="btnDelete" style="height:25px;margin-top:5px">Xóa</button>
            </td>
        </tr>

        <tr>
            <td colspan="9">
                @if (Model.Data.Any())
                {
                    @Html.Partial("_PaginationView", pagNar)
                }
            </td>
        </tr>
    </tfoot>
</table>
<div id="navigate_bottom_panel" class="nav-bottom ">
</div>
<div id="popup-container"></div>

<script>
    @functions{
            public string GetAntiForgeryToken()
            {
                string cookieToken, formToken;
                AntiForgery.GetTokens(null, out cookieToken, out formToken);
                return cookieToken + "," + formToken;
            }
        }
</script>
<script type="text/javascript">

    function OnCriteria(pageIndex) {
        var account = $('#input-account');
        //var software = $('#input-software');
        var inputFunction = $('#input-function');
        var action = $('#input-action');
        var startDate = $('#start-date-search').val();
        var endDate = $('#end-date-search').val();        
        var appId = $('#app-id').val().trim();        
        account = account != null ? account.val().trim() : "";
        //software = software!= null ? software.val().trim() : "";
        inputFunction = inputFunction != null ? inputFunction.val().trim() : "";
        action = action != null ? action.val().trim() : "";
        
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/Log/ActionSearch',
            contentType: "application/json",
            data: JSON.stringify({
                pageIndex: pageIndex,
                model: {
                    Account: account,
                    //SoftWare: software,
                    Function: inputFunction,
                    Action: action,
                    StartDate: startDate,
                    EndDate: endDate,
                    AppId : appId
                }
            }),
            success: function (response) {
                var $catlist = $('#container-list');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }

    $(document).ready(function () {
        $('a').tipsy();

        $("table.tbl-user-list").colResizable({
            headerOnly: true,
            defaultWith: ["auto", "auto", "auto", "auto", "60px", '30px', '30px', '30px']
        });      

        $('#btnDelete').click(function () {
            var values = [];
            $('#checkbox input:checked').each(function () {
                values.push(this.name);
            });
            if (values.length != 0) {
                var $self = $(this);
                comfirmDelete("Bạn có muốn xóa bản ghi này không?", function () {
                    $.ajax("/Log/ActionDelete", {
                        type: "post",
                        contentType: "application/json",
                        data: JSON.stringify({ listID: values }), // JSON data goes here
                        dataType: "json",
                        headers: {
                            'VerificationToken': '@GetAntiForgeryToken()'
                        },
                        success: function (response) {
                            if (response.status == 200) {
                                OnCriteria(0);
                                window.notice(response.result, window.notice_success);
                            }
                            else {
                                window.notice(response.result, window.notice_error);
                            }
                        },
                    });
                }, null, "Xóa log");
            } else {
                warning("Cần chọn bản ghi để xóa", "Xóa log");
            }
        });

        $('.view-detail').bind('click', function (event) {
            //$('.ui-dialog ui-widget ui-widget-content ui-corner-all ui-draggable ui-dialog-buttons').show();
            //$("body").on('click', function (e) {
            //    $(".ui-dialog .ui-dialog-titlebar-close").click();
            //    $('body').off('click');
            //});
            //$(".ui-dialog").on('click', function (e) {
            //    e.stopPropagation();
            //});
            //event.stopPropagation();
            var $self = $(this);
            prepareViewDetail($self.attr('data-id'), 'Chi tiết');
            hideLoading();
        });
    });
    
    function prepareViewDetail(id, dlgTitle) {
        $('#popup-container')
              .load("/Log/ViewDetail", { id: id}, function () {
                  $(this).dialog({
                      draggable: true,
                      modal: true,
                      resizable: false,
                      width: 'auto',
                      title: dlgTitle,
                      minHeight: 75,
                      my: "center",
                      at: "center",
                      of: window,
                      buttons: [
                            {
                                text: "Đóng",
                                icons: {
                                    primary: "ui-icon-close"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    $(this).dialog("close");
                                }
                            }
                      ]
                  });
                  $(".loading").hide();
              });
    }
</script>

